//
//  Hot_rodViewController.m
//  CarOfHome2
//
//  Created by dlios on 15/9/23.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "Hot_rodViewController.h"
#import "Hot_rodTableViewCell.h"
#import "DetailsViewController.h"//详情页
#import "Hot_rodModel.h"
#import <MJRefresh.h>

@interface Hot_rodViewController () <UITableViewDataSource, UITableViewDelegate>


@property (nonatomic, strong) NSMutableArray *arr;
@property (nonatomic, assign) NSInteger count;//加载计数
@property (nonatomic, strong) MBProgressHUD *HUD;

@end

@implementation Hot_rodViewController

- (void)viewWillAppear:(BOOL)animated
{
    
    AppDelegate *dele = [[UIApplication sharedApplication]delegate];
    if (dele.isNight == 1) {
        self.view.backgroundColor = [UIColor blackColor];
        self.tableView.backgroundColor = [UIColor blackColor];
        [self.tableView reloadData];
    }
    else{
        self.view.backgroundColor = [UIColor whiteColor];
        self.tableView.backgroundColor = [UIColor whiteColor];
        [self.tableView reloadData];
    }
    //显示navigationBar、tabBar
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = NO;
    
    if (self.arr.count == 0)
    {
        [self dataHandle];
        [self Open];
        //马上进入刷新状态
        [self.tableView.header beginRefreshing];
    }

    
}


#pragma mark -
- (void)Open
{
    self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:self.HUD];
    self.HUD.labelText = @"加载中..";
    [self.HUD show:YES];
    self.view.userInteractionEnabled = NO;
    
}
- (void)Close
{
    [self.HUD hide:YES afterDelay:2];
    self.view.userInteractionEnabled = YES;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"list"] style:UIBarButtonItemStylePlain target:self action:@selector(presentLeftMenuViewController:)];
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"BBJJ"]];
    
    //加载计数
    self.count = 2;
    
    [self logo];
    [self tableViewSubView];
    [self upLoading];

}


- (void)logo
{
    self.title = @"牛车改装";
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
//    self.navigationController.navigationBar.barStyle = 1;
    
    
}



- (void)dataHandle
{
    
    //刷新数据
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        NSMutableString *url = [NSMutableString stringWithFormat:@"%s%s",Hot_rod1,Hot_rod2];
        [url appendString:@"1"];
        NSMutableArray *mArr = [NSMutableArray array];
        [HTTPTool get:url body:nil httpResult:JSON success:^(id result) {
            NSMutableArray *array = result[@"cases"];
            for (NSDictionary *dic in array)
            {
                Hot_rodModel *model = [[Hot_rodModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [mArr addObject:model];
            }
            self.arr = [NSMutableArray arrayWithArray:mArr];
            [self.tableView reloadData];
            [self Close];
            [self.tableView.header endRefreshing];
        } failure:^(NSError *error) {
            [self Close];
            [self.tableView.header endRefreshing];
            NSLog(@"%@", error);
        }];
    }];
}

//结束刷新状态
- (void)endRefresing
{
    [self.tableView reloadData];
}

//数据加载
- (void)upLoading
{
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        NSString *str = [NSString stringWithFormat:@"http://app.api.niuche.com/modi/cases?page=%ld&count=20&city=", (long)self.count];
        [HTTPTool get:str body:nil httpResult:JSON success:^(id result) {
            NSMutableArray *array = result[@"cases"];
            for (NSDictionary *dic in array)
            {
                Hot_rodModel *model = [[Hot_rodModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.arr addObject:model];
            }
            if (self.count <= 26)
            {
                self.count ++;
            }
            [self.tableView reloadData];
            [self.tableView.footer endRefreshing];
        } failure:^(NSError *error) {
            [self.tableView.footer endRefreshing];
            NSLog(@"%@", error);
        }];
    }];
}

- (void)tableViewSubView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - SAWindth * 64) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.rowHeight = SAHeight * 250;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    //注册cell
    [self.tableView registerClass:[Hot_rodTableViewCell class] forCellReuseIdentifier:@"reuse"];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Hot_rodTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    //cell 风格
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    Hot_rodModel *model = self.arr[indexPath.row];
    cell.hot = model;
    return cell;
}

/* 选中方法 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailsViewController *details = [[DetailsViewController alloc] init];
    Hot_rodModel *model = self.arr[indexPath.row];
    details.numArr = model.pictures;
    details.index = indexPath.row;
    [self.navigationController pushViewController:details animated:YES];
    

}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/



@end
